package com.hengtiansoft.lfy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对话adapter
 */

public class OnDialogueTalkAdapter extends BaseAdapter{
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mList;
    OneMobileChatAdapterListener mListener;
    private final int TYPE_1 = 0; // 类型1
    private final int TYPE_2 = 1; // 类型2
    private final int TYPE_3 = 2; // 类型2
    private int mPostion = -1;

    public void setAdapterListener(OneMobileChatAdapterListener listener) {
        this.mListener = listener;
    }

    public OnDialogueTalkAdapter(Context context,
                                ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
    }

    public class ViewHolder1 {
        EditText mEdtText;
        TextView mTvTextTranslate;
        ImageView mIvPlayer,iv_player_y;
        ImageView mIvTra;
    }

    public class ViewHolder2 {
        EditText mEdtText;
        TextView mTvTextTranslate;
        ImageView mIvPlayer,iv_player_y;
        ImageView mIvTra;
    }

    public class ViewHolder3 {
    }

    @Override
    public int getCount() {
        return mList.size() + 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        // 根据标志，返回布局类型，也就是左右布局
        if (position < getCount() - 1) {
            final HashMap<String, Object> item = getItem(position);
            if (item.get("from").toString().equals("left")) {
                return TYPE_1;
            } else {
                return TYPE_2;
            }
        } else {
            return TYPE_3;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        int id = getCount() - 1;
        ViewHolder3 holder3 = null;
        int type = getItemViewType(position);
        if (position < id) {
            final HashMap<String, Object> item = getItem(position);
            ViewHolder1 holder1 = null;
            ViewHolder2 holder2 = null;
            if (convertView == null) {
                switch (type) {
                    case TYPE_1:
                        holder1 = new ViewHolder1();
                        convertView = mInflater.inflate(
                                R.layout.item_left_listitem_dialoguetalk_activity,
                                null);
                        holder1.mEdtText = (EditText) convertView
                                .findViewById(R.id.tv_text);
                        holder1.mTvTextTranslate = (TextView) convertView
                                .findViewById(R.id.tv_text_translate);
                        holder1.mIvPlayer = (ImageView) convertView
                                .findViewById(R.id.iv_player_translation);
                        holder1.mIvTra = (ImageView) convertView
                                .findViewById(R.id.iv_submit);
                        holder1.iv_player_y= (ImageView) convertView.findViewById(R.id.iv_player_y);
                        convertView.setTag(holder1);
                        break;
                    case TYPE_2:
                        holder2 = new ViewHolder2();
                        convertView = mInflater.inflate(
                                R.layout.item_right_listitem_dialoguetalk_activity,
                                null);
                        holder2.mEdtText = (EditText) convertView
                                .findViewById(R.id.tv_text);
                        holder2.mTvTextTranslate = (TextView) convertView
                                .findViewById(R.id.tv_text_translate);
                        holder2.mIvPlayer = (ImageView) convertView
                                .findViewById(R.id.iv_player_translation);
                        holder2.mIvTra = (ImageView) convertView
                                .findViewById(R.id.iv_submit);
                        holder2.iv_player_y=(ImageView) convertView.findViewById(R.id.iv_player_translation_Y);
                        convertView.setTag(holder2);
                        break;
                    // case TYPE_3:
                    // holder3 = new ViewHolder3();
                    // convertView = mInflater.inflate(
                    // R.layout.item_last_listitem_one_mobile_activity, null);
                    // convertView.setTag(holder3);
                    // break;
                    default:
                        break;
                }
            } else {
                switch (type) {
                    case TYPE_1:
                        holder1 = (ViewHolder1) convertView.getTag();
                        break;
                    case TYPE_2:
                        holder2 = (ViewHolder2) convertView.getTag();
                        break;
                    // case TYPE_3:
                    // holder3 = (ViewHolder3) convertView.getTag();
                    // break;
                    default:
                        break;
                }
            }
            switch (type) {
                case TYPE_1:
                    holder1.mEdtText.setText((String)item.get("text"));
                    holder1.mTvTextTranslate.setText(item.get("translation")
                            .toString());
                    if (mPostion == position) {
                        holder1.mIvTra.setVisibility(View.VISIBLE);
                        holder1.mEdtText.setFocusable(true);
                        holder1.mEdtText.requestFocus();
                        holder1.mEdtText.setSelection(holder1.mEdtText.getText()
                                .length());
                    } else {
                        holder1.mIvTra.setVisibility(View.GONE);
                    }
                    holder1.mIvPlayer.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (mListener != null)
                                mListener.onPlayer(position);
                        }
                    });
                    holder1.iv_player_y.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View view) {
                            if (mListener != null)
                                mListener.onPlayerY(position);
                        }
                    });

                    holder1.mTvTextTranslate
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (mListener != null)
                                        mListener.onPlayer(position);
                                }
                            });
                    holder1.mIvTra.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (mListener != null)
                                mListener.onSubmitClick(position);
                        }
                    });
              /*      holder1.mEdtText.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                if (mListener != null)
                                    mListener.onTouchClick(position);
                            }
                            return false;
                        }
                    });*/
                    holder1.mEdtText.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mListener != null)
                                mListener.onTouchClick(position);
                        }
                    });
                    final View finalConvertView = convertView;
                    holder1.mEdtText.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(true,position,finalConvertView);
                            return true;
                        }
                    });
                    final View finalConvertView1 = convertView;
                    holder1.mTvTextTranslate.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(true,position,finalConvertView1);
                            return true;
                        }
                    });
                    final View finalConvertView5 = convertView;
                    convertView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(true,position,finalConvertView5);
                            return true;
                        }
                    });
//                    holder1.mEdtText.setOnClickListener(new OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            if (mListener != null)
//                                mListener.onPlayerY(position);
//                        }
//                    });
                    holder1.mEdtText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                                mListener.onUpdateText(position);

                            return false;
                        }
                    });
                    break;
                case TYPE_2:
                    holder2.mEdtText.setText(item.get("text").toString());
                    holder2.mTvTextTranslate.setText(item.get("translation")
                            .toString());
                    if (mPostion == position) {
                        holder2.mIvTra.setVisibility(View.VISIBLE);
                        holder2.mEdtText.setSelection(holder2.mEdtText.getText()
                                .length());
                        holder2.mEdtText.setFocusable(true);
                        holder2.mEdtText.requestFocus();
                    } else {
                        holder2.mIvTra.setVisibility(View.GONE);
                    }
                    holder2.mIvPlayer.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (mListener != null)
                                mListener.onPlayer(position);
                        }
                    });
                    holder2.iv_player_y.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mListener != null)
                                mListener.onPlayerY(position);
                        }
                    });
                    holder2.mTvTextTranslate
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (mListener != null)
                                        mListener.onPlayer(position);
                                }
                            });
                    holder2.mIvTra.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (mListener != null)
                                mListener.onSubmitClick(position);
                        }
                    });
                   /* holder2.mEdtText.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                if (mListener != null)
                                    mListener.onTouchClick(position);
                            }
                            return false;
                        }
                    });*/
                    holder2.mEdtText.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mListener != null)
                                mListener.onTouchClick(position);
                        }
                    });

                    holder2.mEdtText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                                    mListener.onUpdateText(position);
                                return false;
                        }
                    });

                    final View finalConvertView3 = convertView;
                    holder2.mEdtText.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(false,position,finalConvertView3);
                            return true;
                        }
                    });

                    final View finalConvertView2 = convertView;
                    holder2.mTvTextTranslate.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(false,position,finalConvertView2);
                            return true;
                        }
                    });
                    final View finalConvertView4 = convertView;
                    convertView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            mListener.onLongClick(false,position,finalConvertView4);
                            return true;
                        }
                    });
//                    holder2.mEdtText.setOnClickListener(new OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            if (mListener != null)
//                                mListener.onPlayerY(position);
//                        }
//                    });
                    break;
            }
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (mListener != null)
                        mListener.onItemClick(position);
                }
            });
        } else {
            Log.i("OneMobile", "holder3");
            if (convertView == null) {
                switch (type) {
                    case TYPE_3:
                        Log.i("OneMobile", "TYPE_3");
                        holder3 = new ViewHolder3();
                        convertView = mInflater.inflate(
                                R.layout.item_last_listitem_dialoguetalk_activity,
                                null);
                        convertView.setTag(holder3);
                        break;
                }
            } else {
                switch (type) {
                    case TYPE_3:
                        holder3 = (ViewHolder3) convertView.getTag();
                        break;
                }
            }
        }
        return convertView;
    }

    /**
     * 设置按钮的可见性
     *
     * @param mposition
     */
    public void setIvTraVisible(int mposition) {
        mPostion = mposition;
    }

    public interface OneMobileChatAdapterListener {

        public void onPlayer(int position);
        public void onPlayerY(int position);

        public void onItemClick(int position);

        public void onSubmitClick(int position);

        public void onLongClick(boolean left,int pos,View view);

        public void onUpdateText(int position);

        public void TextChangedListener(int position);

        public void onTouchClick(int position);
    }
}
